package dao.mysql;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import modelInterfaces.AbstractModelFactory;
import modelInterfaces.IEvent;
import modelInterfaces.IEventType;
import modelInterfaces.IValue;
import dao.DAOException;
import dao.IValueDAO;

public class ValueMySqlDAO implements IValueDAO {

	private String PROPERTIES_FILE_PATH = "config/MySqlDAO.props";
	
	private String TABLE_NAME = "value";
	private String ID_COLUMN_NAME = "id";
	private String EVENT_ID_COLUMN_NAME = "event";
	private String VALUE_COLUMN_NAME = "value";
	
	public ValueMySqlDAO() {
		Properties prop = new Properties();
		try {
			prop.load(new FileInputStream(PROPERTIES_FILE_PATH));
	        TABLE_NAME = prop.getProperty("VALUE_TABLE_NAME") != null ?						prop.getProperty("VALUE_TABLE_NAME") :			this.TABLE_NAME;
	        ID_COLUMN_NAME = prop.getProperty("VALUE_ID_COLUMN_NAME") != null ?				prop.getProperty("VALUE_ID_COLUMN_NAME") :		this.ID_COLUMN_NAME;
	        EVENT_ID_COLUMN_NAME = prop.getProperty("VALUE_EVENT_ID_COLUMN_NAME") != null ?	prop.getProperty("VALUE_EVENT_ID_COLUMN_NAME") :this.EVENT_ID_COLUMN_NAME;
	        VALUE_COLUMN_NAME = prop.getProperty("VALUE_VALUE_COLUMN_NAME") != null ?		prop.getProperty("VALUE_VALUE_COLUMN_NAME") :	this.VALUE_COLUMN_NAME;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public List<IValue> retrieve(IEvent event) throws DAOException {
		String query = "SELECT * FROM "+ TABLE_NAME +" where "+ EVENT_ID_COLUMN_NAME +"='"+event.getId()+"'";
        ResultSet rs = MySqlDBConnection.getInstance().executeQuery(query);
        List<IValue> valueList = new ArrayList<IValue>();
        try
        {
            while (rs.next())
            {
            	IValue value = AbstractModelFactory.getModelFactory().getValue();
                value.setId(rs.getInt(ID_COLUMN_NAME));
                value.setValue(rs.getString(VALUE_COLUMN_NAME));
                valueList.add(value);
            }
        }
        catch (SQLException ex)
        {
            throw new DAOException(ex.getErrorCode(), ex.getMessage());
        }
        return valueList;
	}

	@Override
	public List<IValue> retrieveAll() throws DAOException {
		// TODO [programador] Auto-generated method stub
		return null;
	}
	
	@Override
	public void create(IValue value) throws DAOException {
		// TODO [programador] Auto-generated method stub

	}

	@Override
	public void delete(IValue value) throws DAOException {
		// TODO [programador] Auto-generated method stub

	}

	@Override
	public void update(IValue value) throws DAOException {
		// TODO [programador] Auto-generated method stub

	}

}
